'use strict'

let fs = require('fs');
let path = require('path');
let router = require('koa-router')();

function regContFiles(files){
    files.forEach(item=>{
        let contro = path.join(__dirname,item);
        let tmpPath = contro;
        let routes = require(tmpPath);
        for(let key in routes){
            let r = routes[key][0];
            let fn = routes[key][1];
            if(r === 'get'){
                router.get(key,fn);
            }else{
                router.post(key,fn);
            }
        }
    })
}

module.exports = function(currenDir){
    let defaultDir = currenDir || __dirname;
    let resultPath = regContFiles(defaultDir);
    regContFiles(resultPath);
    return router.routes();
}